<template>
    <div class="login">
        <div class="login_top">
            <div class="login_top_back">
				<!-- @click="$router.push('/self')" -->
                <!-- <div class="back_icon"></div> -->
            </div>
        </div>
        <div class="login_logo">
            <div class="login_login_icon"></div>
        </div>
        <div class="shop_user_form">
            <div class="s_u_f_label">
                <div class="suf_l_title">
                    <div class="sufl_t_test">手机号码</div>
                </div>
                <div class="suf_l_inputCon">
                    <input class="suf_l_input" maxlength="11" type="text" v-model="loginName">
                </div>
            </div>
            <div class="s_u_f_label">
                <div class="suf_l_title">
                    <div class="sufl_t_test">登录密码</div>
                </div>
                <div class="suf_l_inputCon">
                    <input class="suf_l_input" maxlength="16" type="password" v-model="loginPwd">
                </div>
            </div>
            <div class="suf_otherbtn">
                <span @click="e_gopage('/register')">立即注册</span>
                <span @click="e_gopage('/forget')">忘记密码？</span>
            </div>
            <div class="suf_subBtn" :disabled='isAttack' @click="e_login">
                <img class="suf_subBtnImg" :src="btnImg" alt="">
            </div>
        </div>

        <!-- 加载中 -->
        <div class="demo-spin-container" v-if="loadding">
            <Spin fix></Spin>
        </div>

    </div>
</template>

<script>
import NavBar from '@c/navbar/navbar'
import LoginBtn from '@imgs/login/login_btn.png'
import sha256 from 'js-sha256'
import { vpost, sonpost, fpost, gpost, vlink } from '@api/axios'
export default {
	name: 'login',
	data() {
		return {
			btnImg: LoginBtn,
			isAttack: false, //防连点
			loadding: false,
			loginName: '', //手机号
			loginPwd: '', //登录密码
		}
	},

	components: {},

	computed: {},

	methods: {
		// 跳转页面
		e_gopage(p) {
			this.$router.push(p)
		},
		// =======================================执行登录👇===================================//
		// 登录前检查
		beforeCheck() {
			if (this.loginName == '') {
				this.$Message.warning('请输入手机号')
				return
			}
			if (this.loginPwd == '') {
				this.$Message.warning('请输入登录密码')
				return
			}
			// 所有项输入后执行登录
			this.doLogin()
		},
		// 执行登录
		doLogin() {
			let loginData = {
				username: this.loginName,
				password: sha256(this.loginPwd),
				// password: this.loginPwd,
			}
			this.isAttack = true // 仿连点开启
			this.loadding = true
			sonpost(vlink.sign.in, loginData).then(re => {
				this.isAttack = false //仿连点关闭
				this.loadding = false
				if (re.status == 0) {
					//登录成功
					this.$Message.success('登录成功')
					//登录成功更改登录状态
					this.$store.state.signed = true
					// 登录成功返回上级页面
					this.$router.push('/self')
					// 登录成功保存用户基础数据
					this.saveLocal(re.data)
					console.log(re)
				} else {
					//登录失败
					this.$Message.success(re.msg)
				}
			})
		},
		// 登录成功保存LocalStorage
		saveLocal(re) {
			console.log(re)
			let localdata = {
				accessToken: re.accessToken,
				userId: re.userId,
				loginName: this.loginName,
				loginPwd: sha256(this.loginPwd),
                signed: 1,
				encodePath:re.encodePath,
				ygMemberIdentityId: re.ygMemberIdentityId,
				proxyState: re.state,
				tradPassword: re.tradPassword
			}
			let fordata = ['accessToken', 'userId', 'loginName', 'loginPwd', 'signed','encodePath', 'ygMemberIdentityId', 'proxyState', 'tradPassword']
			fordata.forEach(v => {
				localStorage[v] = localdata[v]
			})
		},
		// 登录
		e_login() {
			this.beforeCheck()
		},
		// =======================================执行登录👆===================================//
	},

	mounted() {},
}
</script>